Arrays in C

Arrays are collection of items of same type which are stored at contiguous memory locations.

Suppose if we want to store the collection of employee numbers then we can used the array.

Array stores the values starting from index value 0 and stores the values in consecutive(next) position in the array.

array memory

Here the size of the variable arr is 10, which starts from the index position 0 and ends with 9. The values are stored in arr[0], arr[1] through arr[9].

Syntax

data-type variable-name[size];

Example

int arr[10];

Here we have declared an array arr of type int and with size 10. So now we can store 10 integer values inside the arr variable.

There are 2 ways to initialize an array

*) Compile Time Initialization

*) Runtime Time Initialization

Compile Time Initialization

If we assign values to the array initially then we call it as compile time initialization.

Example

int marks[5]={ 64, 82, 55, 71, 65 };  
float area[5]={ 23.4, 6.8, 5.5 }; 

If we try to initialize more values than the array size then it will through an error.

Example

int marks[4]={ 64, 82, 55, 71, 65 }; // Compile time error

This will throw compile time error.

Example program to print the values in an array

#include<stdio.h>
void main()
{
    int i;
// Compile time array initialization
int arr[] = {1,2, 3, 4,5};      
    for(i = 0 ; i < 3 ; i++) 
    {
        printf("%d\t",arr[i]);
    }
}

Output

1 2 3 4 5

Runtime Array Initialization

If we declare the array initially and assign the values during run time of the program using scanf() function then its called Runtime Array Initialization.

Example

#include<stdio.h>

void main()
{
    int size = 5;
    int arr[size];
    int i, j;
    printf("Enter the 5 numbers");
    for(i = 0; i < size; i++)
    {
        // Run time array initialization
        scanf("%d", &arr[i]);    
    }
    printf("Values entered are");
    for(j = 0; j < size; j++)
    {
        printf("%d\n", arr[j]);
    }
}

Output

Enter the 5 numbers
1
2
3
4
5
Values entered are
1
2
3
4
5

Multi-Dimensional Array

Multidimensional array is also known as array of arrays. The data in the multidimensional array is stored in the form of tabular form. So its in the format of rows and columns. The rows and columns begin with the index of 0.

Syntax

data-type array-name[row-size][column-size];

Example

int a[3][3];

list of array

Inorder to declare and initialise a 2-dimensional array,

int a[3][3] = {
    {0,0,0},
    {1,1,1},
    {2,2,2}
};

Here we have declared and intialized an 2-dimensional array with 3*3 matrix. So now a[0][0] will hold 0, a[1][0] will hold 1 and a[1][0] will hold 2 and so on.

Example

#include<stdio.h>

void main()
{
    int arr[3][3];
    int i, j;
    // Get the 3*3 matrix
    printf("Enter the 3*3 matrix");
    for(i = 0; i < 3;i++)
    {
        for(j = 0; j < 3; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
    printf("Result matrix is\n");
    // Print the 3*3 matrix
    for(i = 0; i < 3; i++)
    {
        for(j = 0; j < 3; j++)
        {
            printf("%d\n", arr[i][j]);
        }
        printf("\n");
    }
}

Here in the first nested for loop we get the 3*3 matrix as input using scanf() function. Now with the next nested for loop we will display the result in matrix format.

Output

Enter the 3*3 matrix
1
2
3
4
5
6
7
8
9
Result matrix is
1 2 3
4 5 6
7 8 9

Most Read